package com.qingke.easyjava.jdbc.command.impl;

import com.qingke.easyjava.jdbc.app.BeSuperDao;
import com.qingke.easyjava.jdbc.command.PlayerCommand;
import com.qingke.easyjava.jdbc.pojo.Answer;
import com.qingke.easyjava.jdbc.pojo.Player;
import com.qingke.easyjava.jdbc.pojo.Question;
import com.qingke.easyjava.jdbc.util.QingkeConsole;

public class AnswerCommand extends PlayerCommand {

    public AnswerCommand(Player player) {
        super(player);
    }

    @Override
    public void executePlayerCommand() {
        int qid = QingkeConsole.askUserInputInt("请输入问题ID");
        
        BeSuperDao dao = new BeSuperDao();
        Question question = dao.getQuestion(qid);
        
        if (question == null) {
            QingkeConsole.println("您输入的问题ID不存在！");
            return;
        }
        
        if (question.getFrom().getId() == player.getId()) {
            QingkeConsole.println("您不允许自己提出的问题！");
            return;
        }

        String content = QingkeConsole.askUserInput("请输入您的回答");
        Answer answer = new Answer(-1, content, player);
        dao.upsertAnwser(answer);
        
        player.getAnswers().add(answer);
        question.getAnswers().add(answer);
        
    }

}
